//
//  ShopDecorationViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/22.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "ShopDecorationViewController.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import "CompanyTypeModel.h"
#import "CloseGoodsAddressViewController.h"
#import "LogisticsLocationViewController.h"
#import "DetailsModel.h"

static NSString *cellPath = @"MyCarCell";
@interface ShopDecorationViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate,UIScrollViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UITextViewDelegate>
{
    UIPickerView *pickTypeView;
    UITextView *conView;

}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic,strong)UIView *TypeView;
@property (nonatomic, strong)NSArray *titAry;
@property (nonatomic, strong)NSArray *allTypeAry;
@property (nonatomic, strong)NSString *serviceTypeStr;
@property (nonatomic, strong)NSString *serviceTypeID;
@property (nonatomic, strong)NSString *fullName;
@property (nonatomic, strong)NSString *ReferredName;
@property (nonatomic, strong)NSString *contact;
@property (nonatomic, strong)NSString *phoneStr;
@property (nonatomic, strong)NSString *MachineStr;
@property (nonatomic, strong)NSString *areaStr;
@property (nonatomic, strong)NSString *addressStr;
@property (nonatomic, strong)NSString *websiteStr;
@property (nonatomic, strong)NSString *businStr;
@property (nonatomic, strong)NSString *pladerStr;

@property (nonatomic, strong)NSString *logoImgUrl;
@property (nonatomic, strong)NSString *enterpriseImgUrl;
@property (nonatomic, strong)NSString *businlicImgUrl;
@property (nonatomic, strong)NSString *transportImgUrl;
@property (nonatomic, strong)NSMutableArray *bannerAry;
@property (nonatomic, strong)NSMutableArray *companyAry;
@property (nonatomic, strong)NSMutableArray *contentAry;

@property (nonatomic, strong)NSString *latitude;
@property (nonatomic, strong)NSString *lontude;
@property (nonatomic, strong)NSString *streeID;
@property (nonatomic, strong)DetailsModel *detaModel;

@property (nonatomic, assign)NSInteger imgType;
@property (nonatomic, assign)NSInteger lenthType;


@end

@implementation ShopDecorationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titAry = @[@"服务类型",@"公司全称",@"公司简称",@"联系人",@"手机号",@"座机号",@"公司地址",@"公司官网",@"主营业务",@"公司简介"];
    [self setAllName];
    [self creatUI];
    [self requestTruckVerifyInfo];
    [self requestCompanyInType];
}

- (void)setAllName
{
    _imgType = 0;
    _serviceTypeStr = @"";
    _serviceTypeID = @"";
    _fullName = @"";
    _ReferredName = @"";
    _contact = @"";
    _phoneStr = @"";
    _MachineStr = @"";
    _areaStr = @"";
    _addressStr = @"";
    _websiteStr = @"";
    _businStr = @"";
    _pladerStr = @"请用尽量多的文字，详细的描述您公司的企业文化...";
    
    _logoImgUrl = @"";
    _enterpriseImgUrl = @"";
    _businlicImgUrl = @"";
    _transportImgUrl = @"";
    _bannerAry = [NSMutableArray arrayWithCapacity:0];
    _companyAry = [NSMutableArray arrayWithCapacity:0];
    _contentAry = [NSMutableArray arrayWithCapacity:0];
    _streeID = @"";
    _latitude = @"";
    _lontude = @"";
}

- (void)creatUI
{
    self.navigationItem.title = @"店铺装修";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    [self.view addSubview:_tableview];
}

- (void)companyAction
{
    if (!self.TypeView)
    {
        self.TypeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT)];
        _TypeView.backgroundColor = RGBA(0, 0, 0, 0.6);
        [self.view addSubview:_TypeView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bottomViewPressed)];
        [_TypeView addGestureRecognizer:tap];
        pickTypeView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-250*displayheight-STA_NAV_HEIGHT, SCREEN_WIDTH, 250*displayheight)];
        pickTypeView.backgroundColor = [UIColor whiteColor];
        pickTypeView.delegate = self;
        pickTypeView.dataSource = self;
        [_TypeView addSubview:pickTypeView];
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-280*displayheight-STA_NAV_HEIGHT, SCREEN_WIDTH, 30*displayheight)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [_TypeView addSubview:whiteView];
        UIButton *cancelBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 10*displayheight, 40*displaywidth, 20*displayheight) ImageName:nil Target:self Action:@selector(bottomViewPressed) Title:@"取消" Font:13.0];
        cancelBtn.backgroundColor = ThemeColor;
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelBtn.layer.cornerRadius = 3;
        cancelBtn.layer.masksToBounds = YES;
        [whiteView addSubview:cancelBtn];
        
        UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH- 55*displaywidth, 10*displayheight, 40*displaywidth, 20*displayheight) ImageName:nil Target:self Action:@selector(soureAction) Title:@"确定" Font:13.0];
        soureBtn.backgroundColor = ThemeColor;
        [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        soureBtn.layer.cornerRadius = 3;
        soureBtn.layer.masksToBounds = YES;
        [whiteView addSubview:soureBtn];
        
    }
    _TypeView.hidden = NO;
    
}

- (void)soureAction
{
    NSInteger selectRow = [pickTypeView selectedRowInComponent:0];
    CompanyTypeModel *model = _allTypeAry[selectRow];
    _serviceTypeID = s_Integer(model.ID);
    _serviceTypeStr = model.name;
    [self bottomViewPressed];
    [_tableview reloadData];
}


- (void)bottomViewPressed
{
    _TypeView.hidden = YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _allTypeAry.count;
}

#pragma mark UIPickerView Delegate Method 代理方法

//指定每行如何展示数据（此处和tableview类似）
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    CompanyTypeModel *model = _allTypeAry[row];
    return model.name;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%ld------%ld",component,row);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return _titAry.count;
    }
    else if (section == 1)
    {
        return 5;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0)
    {
        cell.backgroundColor = BacksColor;
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 45*displaywidth)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:whiteView];
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(0, 5*displaywidth, 95*displaywidth, 35*displaywidth) Font:15.0 Text:_titAry[indexPath.row] Color:BlaTitColor];
        titLab.textAlignment = NSTextAlignmentRight;
        [whiteView addSubview:titLab];
        if (indexPath.row != 9)
        {
            UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(100*displaywidth, 12.5*displaywidth, 1, 20*displaywidth)];
            line1.backgroundColor = RGB(204, 204, 204);
            [whiteView addSubview:line1];
            
            if (indexPath.row != 0 && indexPath.row != 9)
            {
                NSArray *textAry = @[@"",_fullName,_ReferredName,_contact,_phoneStr,_MachineStr,_addressStr,_websiteStr,_businStr,@""];
                UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(110*displaywidth, 2.5*displaywidth, SCREEN_WIDTH-125*displaywidth, 40*displaywidth) placeholder:@"" passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
                [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
                textfield.delegate = self;
                textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
                textfield.textColor = BlaTitColor;
                textfield.textAlignment = NSTextAlignmentLeft;
                textfield.text = textAry[indexPath.row];
                textfield.tag = 100+indexPath.row;
                [whiteView addSubview:textfield];
                if (indexPath.row == 6)
                {
                    whiteView.frame = CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 80*displaywidth);
                    textfield.placeholder = @"请补全所在街道及道路名称...";
                    textfield.frame = CGRectMake(15*displaywidth, 45*displaywidth, SCREEN_WIDTH-115*displaywidth, 35*displaywidth);
                    UIImageView *rightImg = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-30*displaywidth, 12.5*displayheight, 15*displaywidth, 20*displayheight) ImageName:@"gs_icon05"];
                    rightImg.contentMode = UIViewContentModeScaleAspectFit;
                    [whiteView addSubview:rightImg];
                    UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(110*displaywidth, 0, SCREEN_WIDTH-180*displaywidth, 45*displaywidth) Font:14.0 Text:_areaStr Color:BlaTitColor];
                    [whiteView addSubview:nameLab];
                    UIButton *reloadBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-95*displaywidth, 50*displaywidth, 80*displaywidth, 25*displaywidth) ImageName:nil Target:self Action:@selector(reloadAddressAction) Title:@"重新定位" Font:13.0];
                    reloadBtn.backgroundColor = ThemeColor;
                    [reloadBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    [whiteView addSubview:reloadBtn];
                }
            }
            else if (indexPath.row == 0)
            {
                UIImageView *rightImg = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-30*displaywidth, 12.5*displayheight, 15*displaywidth, 20*displayheight) ImageName:@"gs_icon05"];
                rightImg.contentMode = UIViewContentModeScaleAspectFit;
                [whiteView addSubview:rightImg];
                UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(110*displaywidth, 0, SCREEN_WIDTH-180*displaywidth, 45*displaywidth) Font:14.0 Text:_serviceTypeStr Color:BlaTitColor];
                [whiteView addSubview:nameLab];
               
            }
        }
        else
        {
            whiteView.frame = CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 190*displaywidth);
            UITextView *contentAView = [[UITextView alloc] initWithFrame:CGRectMake(15*displaywidth, 45*displaywidth, SCREEN_WIDTH-30*displaywidth, 130*displaywidth)];
            contentAView.delegate = self;
            contentAView.text = _pladerStr;
            contentAView.textColor = BlaTitColor;
            if ([_pladerStr isEqualToString:@"请用尽量多的文字，详细的描述您公司的企业文化..."])
            {
                contentAView.textColor = RGB(179, 179, 179);
            }
            contentAView.backgroundColor = BacksColor;
            contentAView.layer.cornerRadius = 10;
            contentAView.layer.masksToBounds = YES;
            [whiteView addSubview:contentAView];
            conView = contentAView;
        }
        
    }
    else if (indexPath.section == 1)
    {
        cell.backgroundColor = [UIColor whiteColor];
        UIView *lineAView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10*displaywidth)];
        lineAView.backgroundColor = BacksColor;
        [cell.contentView addSubview:lineAView];
        if (indexPath.row != 0)
        {
            lineAView.frame = CGRectMake(15*displaywidth, 0, SCREEN_WIDTH-30*displaywidth, 1*displaywidth);
        }
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 15*displaywidth, 150*displaywidth, 35*displaywidth) Font:15.0 Text:@"" Color:BlaTitColor];
        [cell.contentView addSubview:titLab];
        UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 55*displaywidth, SCREEN_WIDTH-30*displaywidth, 20*displaywidth) Font:13.0 Text:@"" Color:RGB(204, 204, 204)];
        [cell.contentView addSubview:tostLab];
        if (indexPath.row == 0 || indexPath.row == 1)
        {
            NSArray *titleArray = @[@"公司LOGO",@"企业形象图"];
            NSArray *tostArray = @[@"LOGO规格：宽600px 高400px (或者按照3:2比例)",@"用于放在公司简介下方，规格：宽750px 高200px"];
            titLab.text = titleArray[indexPath.row];
            tostLab.text = tostArray[indexPath.row];
            CGFloat width = SCREEN_WIDTH-30*displaywidth;
            UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 80*displaywidth, SCREEN_WIDTH-30*displaywidth, 100*displaywidth)];
            if (indexPath.row == 0)
            {
                backV.frame = CGRectMake(SCREEN_WIDTH/2-80*displaywidth, 90*displaywidth, 160*displaywidth, 100*displaywidth);
                width = 160*displaywidth;
            }
            backV.backgroundColor = RGB(244, 241, 244);
            backV.layer.cornerRadius = 5;
            backV.layer.masksToBounds = YES;
            [cell.contentView addSubview:backV];
            UIImageView *imgV = [ZCControl createImageViewWithFrame:CGRectMake(width/2-22.5*displaywidth, 22.5*displaywidth, 45*displaywidth, 45*displaywidth) ImageName:@"my_icon07"];
            imgV.contentMode = UIViewContentModeScaleAspectFit;
            [backV addSubview:imgV];
            UILabel *namelab = [ZCControl createLabelWithFrame:CGRectMake(0, 70*displaywidth, width, 30*displaywidth) Font:12.0 Text:@"立即上传" Color:RGB(32, 69, 139)];
            namelab.textAlignment = NSTextAlignmentCenter;
            [backV addSubview:namelab];
            NSArray *imgUrlAry = @[_logoImgUrl,_enterpriseImgUrl];
            UIImageView *cardImgview = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, width, 100*displaywidth) ImageName:nil];
            cardImgview.backgroundColor = [UIColor clearColor];
            if (![imgUrlAry[indexPath.row] isEqualToString:@""])
            {
                [cardImgview sd_setImageWithURL:URL(imgUrlAry[indexPath.row])];
            }
            cardImgview.contentMode = UIViewContentModeScaleAspectFill;
            cardImgview.layer.cornerRadius = 5;
            cardImgview.layer.masksToBounds = YES;
            [backV addSubview:cardImgview];
            UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, width, 100*displayheight) ImageName:nil Target:self Action:@selector(pushAction:) Title:nil Font:12.0];
            btn.backgroundColor = [UIColor clearColor];
            btn.tag = 100+indexPath.row;
            [cardImgview addSubview:btn];
        }
        else if (indexPath.row == 2)
        {
            titLab.text = @"公司资质照";
            tostLab.hidden = YES;
            NSArray *imgtitAry = @[@"营业执照",@"运输许可证"];
            CGFloat width = (SCREEN_WIDTH-45*displaywidth)/2;
            NSArray *imgUrlAry = @[_businlicImgUrl,_transportImgUrl];
            for (int i = 0; i < imgtitAry.count; i++)
            {
                UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth+(width+15*displaywidth)*i, 50*displaywidth, width, 100*displaywidth)];
                backV.backgroundColor = RGB(244, 241, 244);
                backV.layer.cornerRadius = 5;
                backV.layer.masksToBounds = YES;
                [cell.contentView addSubview:backV];
                UIImageView *imgV = [ZCControl createImageViewWithFrame:CGRectMake(width/2-22.5*displaywidth, 22.5*displaywidth, 45*displaywidth, 45*displaywidth) ImageName:@"my_icon07"];
                imgV.contentMode = UIViewContentModeScaleAspectFit;
                [backV addSubview:imgV];
                UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(0, 70*displaywidth, width, 30*displaywidth) Font:12.0 Text:imgtitAry[i] Color:RGB(32, 69, 139)];
                titlab.textAlignment = NSTextAlignmentCenter;
                [backV addSubview:titlab];
                UIImageView *cardImgview = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, width, 100*displaywidth) ImageName:nil];
                cardImgview.backgroundColor = [UIColor clearColor];
                if (![imgUrlAry[i] isEqualToString:@""])
                {
                    [cardImgview sd_setImageWithURL:URL(imgUrlAry[i])];
                }
                cardImgview.contentMode = UIViewContentModeScaleAspectFill;
                cardImgview.layer.cornerRadius = 5;
                cardImgview.layer.masksToBounds = YES;
                [backV addSubview:cardImgview];
                UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, width, 100*displayheight) ImageName:nil Target:self Action:@selector(pushAction:) Title:nil Font:12.0];
                btn.backgroundColor = [UIColor clearColor];
                btn.tag = 200+i;
                [cardImgview addSubview:btn];
            }
        }
        else if (indexPath.row == 3 || indexPath.row == 4)
        {
            NSArray *titleArray = @[@"轮播图广告",@"公司相册"];
            NSArray *tostArray = @[@"广告规格：宽600px 高400px",@""];
            titLab.text = titleArray[indexPath.row-3];
            tostLab.text = tostArray[indexPath.row-3];
            CGFloat H = 80*displaywidth;
            CGFloat imgH = 90*displaywidth;
            CGFloat WIDTH = (SCREEN_WIDTH-50*displaywidth)/3;
            NSArray *allImg = [_bannerAry copy];
            if (indexPath.row == 4)
            {
                tostLab.hidden = YES;
                allImg = [_companyAry copy];
                imgH = 120*displaywidth;
                H = 50*displaywidth;
            }
            NSInteger num = allImg.count+1;
            for (int i = 0; i < num; i++)
            {
                if (i == (num-1) && i != 999)
                {
                    UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth+(WIDTH+10*displaywidth)*(i%3),H+imgH*(i/3), WIDTH, 75*displaywidth)];
                    backV.backgroundColor = RGB(244, 241, 244);
                    backV.layer.cornerRadius = 5;
                    backV.layer.masksToBounds = YES;
                    [cell.contentView addSubview:backV];
                    UIImageView *imgV = [ZCControl createImageViewWithFrame:CGRectMake(WIDTH/2-17.5*displaywidth, 10*displaywidth, 35*displaywidth, 35*displaywidth) ImageName:@"my_icon07"];
                    imgV.contentMode = UIViewContentModeScaleAspectFit;
                    [backV addSubview:imgV];
                    UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(0, 45*displaywidth, WIDTH, 30*displaywidth) Font:12.0 Text:@"立即上传" Color:RGB(32, 69, 139)];
                    titlab.textAlignment = NSTextAlignmentCenter;
                    [backV addSubview:titlab];
                    UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, WIDTH, 75*displayheight) ImageName:nil Target:self Action:@selector(addimgAction:) Title:nil Font:12.0];
                    btn.backgroundColor = [UIColor clearColor];
                    btn.tag = 5000+indexPath.row;
                    [backV addSubview:btn];
                }
                else
                {
                    UIImageView *cardImgview = [ZCControl createImageViewWithFrame:CGRectMake(15*displaywidth+(WIDTH+10*displaywidth)*(i%3),H+imgH*(i/3), WIDTH, 75*displaywidth) ImageName:nil];
                    cardImgview.backgroundColor = [UIColor clearColor];
                    if (![allImg[i] isEqualToString:@""])
                    {
                        [cardImgview sd_setImageWithURL:URL(allImg[i])];
                    }
                    cardImgview.contentMode = UIViewContentModeScaleAspectFill;
                    cardImgview.layer.cornerRadius = 5;
                    cardImgview.layer.masksToBounds = YES;
                    [cell.contentView addSubview:cardImgview];
                    UIButton *delectBtn = [ZCControl createButtonWithFrame:CGRectMake(cardImgview.frame.origin.x+cardImgview.frame.size.width-10*displayheight, cardImgview.frame.origin.y-10*displayheight, 20*displayheight, 20*displayheight) ImageName:@"delected11" Target:self Action:@selector(delecteBtn:) Title:nil Font:12.0];
                    delectBtn.tag = 1000*indexPath.row+i;
                    [cell.contentView addSubview:delectBtn];
                    if (indexPath.row == 4)
                    {
                        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(15*displaywidth+(WIDTH+10*displaywidth)*(i%3),H+imgH*(i/3)+77.5*displaywidth, WIDTH, 25*displaywidth) placeholder:@" 文字说明" passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
                        textfield.layer.borderWidth = 1;
                        textfield.layer.borderColor = RGB(204, 204, 204).CGColor;
                        textfield.layer.cornerRadius = 3;
                        textfield.layer.masksToBounds = YES;
                        [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
                        textfield.delegate = self;
                        textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
                        textfield.textColor = BlaTitColor;
                        textfield.textAlignment = NSTextAlignmentLeft;
                        textfield.text = @"";
                        if (_contentAry.count == (num-1))
                        {
                            textfield.text = _contentAry[i];
                        }
                        textfield.tag = 10000*indexPath.row+i;
                        [cell.contentView addSubview:textfield];
                    }
                }
            }
        }
        
       
    }
    else
    {
        cell.backgroundColor = BacksColor;
        UIButton *saveBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 30*displaywidth, SCREEN_WIDTH-30*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(saveBtnAction) Title:@"提交登记" Font:16.0];
        saveBtn.backgroundColor = RGB(255, 153, 0);
        [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        saveBtn.layer.cornerRadius = 5;
        saveBtn.layer.masksToBounds = YES;
        [cell.contentView addSubview:saveBtn];
    }
    
    
    
    return cell;
}

- (void)reloadAddressAction
{
    WEAKSELF;
    LogisticsLocationViewController *locationVC = [[LogisticsLocationViewController alloc] init];
    locationVC.locationBlock = ^(NSString *lat, NSString *lng, NSString *address)
    {
        _latitude = lat;
        _lontude = lng;
        _addressStr = address;
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    };
    [self.navigationController pushViewController:locationVC animated:YES];
}

- (void)delecteBtn:(UIButton *)btn
{
    NSInteger row = btn.tag/1000;
    NSInteger index = btn.tag%1000;
    if (row == 3)
    {
        if (_bannerAry.count > index)
        {
            [_bannerAry removeObjectAtIndex:index];
        }
    }
    else
    {
        if (_companyAry.count > index && _contentAry.count > index)
        {
            [_companyAry removeObjectAtIndex:index];
            [_contentAry removeObjectAtIndex:index];
        }
    }
    [_tableview reloadData];
}

//提交登记哈哈哈
- (void)saveBtnAction
{
    [self.view endEditing:YES];
    NSArray *nameARRAY = @[_serviceTypeStr,_fullName,_ReferredName,_contact,_phoneStr,_MachineStr,_areaStr,_addressStr,_latitude,_websiteStr,_businStr,_pladerStr,_logoImgUrl,_enterpriseImgUrl,_businlicImgUrl,_transportImgUrl];
    NSArray *tostAry = @[@"请选择服务类型",@"请输入公司全称",@"请输入公司简称",@"请输入联系人",@"请输入手机号",@"请输入座机号",@"请选择公司地址",@"请输入公司详细街道信息",@"请对公司地址进行定位，便于地图查询",@"请输入公司官网",@"请输入主营业务",@"请输入公司简介",@"请上传公司LOGO",@"请上传企业形象图",@"请上传营业执照",@"请上传运输许可证",@"",@"",@""];
    for (int i = 0; i < nameARRAY.count; i++)
    {
        if (i != 11)
        {
            if ([nameARRAY[i] isEqualToString:@""])
            {
                [LTools showMBProgressWithText:tostAry[i] addToView:self.view];
                return;
            }
        }
        else
        {
            if ([nameARRAY[i] isEqualToString:@"请用尽量多的文字，详细的描述您公司的企业文化..."])
            {
                [LTools showMBProgressWithText:tostAry[i] addToView:self.view];
                return;
            }
        }
        
    }
    if (_bannerAry.count == 0)
    {
        [LTools showMBProgressWithText:@"请上传轮播图广告" addToView:self.view];
        return;
    }
    if (_companyAry.count == 0)
    {
        [LTools showMBProgressWithText:@"请上传公司相册" addToView:self.view];
        return;
    }
    NSString *bannerStr = [_bannerAry componentsJoinedByString:@","];
    NSMutableArray *companyImgAry = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < _companyAry.count; i++)
    {
        NSString *titStr = @"";
        if (_contentAry.count > i)
        {
            titStr = _contentAry[i];
        }
        NSDictionary *imgDic = @{@"title":titStr,
                                 @"img":_companyAry[i]
                                 };
        [companyImgAry addObject:imgDic];
    }
    NSString *comimgs = [self toReadableJSONString:companyImgAry];
    WEAKSELF;
    NSDictionary *dic = @{@"serve.id":_serviceTypeID,
                          @"companyName":_fullName,
                          @"nameShort":_ReferredName,
                          @"phone":_phoneStr,
                          @"linkman":_contact,
                          @"fixPhone":_MachineStr,
                          @"street.id":s_str(_streeID),
                          @"address":_addressStr,
                          @"lat":_latitude,
                          @"lng":_lontude,
                          @"url":_websiteStr,
                          @"sketch":_businStr,
                          @"describes":_pladerStr,
                          @"logo":_logoImgUrl,
                          @"price":_enterpriseImgUrl,
                          @"businessImg":_businlicImgUrl,
                          @"certificateImg":_transportImgUrl,
                          @"photoImgs":bannerStr,
                          @"companyImgs":comimgs
                          };
    [HYBaseHandler requestWithTheFormUrl:SERVER_companySave Parmers:dic Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             [LTools showMBProgressWithText:@"提交成功，等待审核" addToView:sharedDelegate.window];
             [self.navigationController popViewControllerAnimated:YES];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {

     }];
    
}

- (NSString *)toReadableJSONString:(NSArray *)array
{
    NSData *data = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
    if (data == nil) {
        return nil;
    }
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}


- (void)addimgAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (btn.tag == 5003)
    {
        _imgType = 4;
    }
    else
    {
        _imgType = 5;
    }
    [self creatPhoto];
}



- (void)pushAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (btn.tag == 100)
    {
        _imgType = 0;
    }
    else if (btn.tag == 101)
    {
        _imgType = 1;
    }
    else if (btn.tag == 200)
    {
        _imgType = 2;
    }
    else if (btn.tag == 201)
    {
        _imgType = 3;
    }
    [self creatPhoto];
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    WEAKSELF;
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            [self companyAction];
            [pickTypeView reloadAllComponents];
        }
        else if (indexPath.row == 6)
        {
            CloseGoodsAddressViewController *vc = [[CloseGoodsAddressViewController alloc] init];
            vc.areaType = indexPath.row;
            vc.isPush = YES;
            vc.closeBlock = ^(NSString *name, NSInteger index)
            {
                NSArray *idAry = [name componentsSeparatedByString:@","];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (idAry.count == 2)
                    {
                        _streeID = idAry[1];
                        _areaStr = idAry[0];
                        
                    }
                    [weakSelf.tableview reloadData];
                });
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 6)
        {
            return 90*displaywidth;
        }
        else if (indexPath.row == 9)
        {
            return 200*displaywidth;
        }
        return 55*displaywidth;
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            return 210*displaywidth;
        }
        else if (indexPath.row == 1)
        {
            return 200*displaywidth;
        }
        else if (indexPath.row == 2)
        {
            return 170*displaywidth;
        }
        else
        {
            CGFloat H = 80*displaywidth;
            CGFloat imgH = 90*displaywidth;
            NSArray *allImg = [_bannerAry copy];
            if (indexPath.row == 4)
            {
                allImg = [_companyAry copy];
                imgH = 120*displaywidth;
                H = 50*displaywidth;
            }
            NSInteger num = allImg.count+1;
            NSInteger line = num/3;
            if (num%3 != 0)
            {
                line+=1;
            }
            return H+imgH*line;
        }
    }
    
    return 275*displaywidth+SCREER_HeiX;
}

- (void)creatPhoto
{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //拍照
    WEAKSELF;
    [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        HYLog(@"拍照");
        [weakSelf takePhoto];
        
    }]];
    
    //从相册中选取
    [alertVC addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        HYLog(@"从相册中选取");
        [weakSelf choseImageWithTypeCameraTypePhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];
        
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        HYLog(@"取消");
        [weakSelf dismissViewControllerAnimated:NO completion:nil];
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}


-(void)takePhoto
{
    //    判断是否打开相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您尚未开启相机权限，请去系统设置界面打开相机权限！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication]canOpenURL:url]) {
                [[UIApplication sharedApplication]openURL:url];
            }
        }];
        [alertVC addAction:okAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        
        
    }
    else
    {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            //设置拍照后的图片可被编辑
            picker.allowsEditing = YES;
            picker.sourceType = sourceType;
            [self presentViewController:picker animated:YES completion:nil];
        }else
        {
            
        }
        
    }
}


- (void)choseImageWithTypeCameraTypePhotoLibrary:(UIImagePickerControllerSourceType)type{
    
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate =self;
    imagePicker.sourceType = type;
    imagePicker.allowsEditing = YES;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing =YES;
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    WEAKSELF;
    [HttpTool uploadImageWithPath:@"/file/upload" params:nil thumbName:@"file" image:image success:^(id json) {
        if ([json server_Getcodecheck])
        {
            NSArray *imgAry = json[@"data"];
            if (imgAry.count > 0)
            {
                NSString *imgStr = imgAry[0];
                if (weakSelf.imgType == 0)
                {
                    weakSelf.logoImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 1)
                {
                    weakSelf.enterpriseImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 2)
                {
                    weakSelf.businlicImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 3)
                {
                    weakSelf.transportImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 4)
                {
                    [weakSelf.bannerAry addObject:imgStr];
                }
                else if (weakSelf.imgType == 5)
                {
                    [weakSelf.companyAry addObject:imgStr];
                    [weakSelf.contentAry addObject:@""];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableview reloadData];
                });
            }
        }
    } failure:^(NSError *error) {
        
    } progress:^(CGFloat progress) {
        
    }];
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)requestCompanyInType
{
    WEAKSELF;
    [HYBaseHandler requestCompanyInTypeParmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            _allTypeAry = [CompanyTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


- (void)requestTruckVerifyInfo
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_companyVerifyInfo Parmers:nil Success:^(id obj)
     {
         if ([obj[@"data"] isKindOfClass:[NSString class]])
         {
             return;
         }
         if ([obj[@"data"][@"status"] integerValue] == -1)
         {
             return;
         }
         if ([obj server_Getcodecheck])
         {
             _detaModel = [DetailsModel mj_objectWithKeyValues:obj[@"data"]];
             NSString *tostStr = @"";
             
             if ([_detaModel.status integerValue] == 0)
             {
                 tostStr = @"店铺资料审核中";
             }
             else if ([_detaModel.status integerValue] == 1)
             {
                 tostStr = @"店铺资料已审核通过，重新填写需重新审核";
             }
             else if ([_detaModel.status integerValue] == 2)
             {
                 tostStr = @"店铺资料未能通过，请重新编辑后提交";
             }
             UIAlertController *alerVc = [UIAlertController alertControllerWithTitle:@"提示" message:tostStr preferredStyle:UIAlertControllerStyleAlert];
             [alerVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                 if ([weakSelf.detaModel.status integerValue] == 0)
                 {
                     [weakSelf.navigationController popViewControllerAnimated:YES];
                 }
             }]];
             [self presentViewController:alerVc animated:YES completion:nil];
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf reloadALLname];
                 [weakSelf.tableview reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
}

- (void)reloadALLname
{
    _imgType = 0;
    _serviceTypeStr = _detaModel.serveName;
    _serviceTypeID = s_str(_detaModel.serveID);
    _fullName = _detaModel.companyName;
    _ReferredName = _detaModel.nameShort;
    _contact = _detaModel.linkman;
    _phoneStr = _detaModel.phone;
    _MachineStr = _detaModel.fixPhone;
    _areaStr = [NSString stringWithFormat:@"%@%@%@%@",_detaModel.street.province,_detaModel.street.city,_detaModel.street.district,_detaModel.street.name];
    _addressStr = _detaModel.address;
    _websiteStr = _detaModel.url;
    _businStr = _detaModel.sketch;
    _pladerStr = _detaModel.describes;
    
    _logoImgUrl = _detaModel.logo;
    _enterpriseImgUrl = _detaModel.price;
    _businlicImgUrl = _detaModel.businessImg;
    _transportImgUrl = _detaModel.certificateImg;
    NSArray *banAry = [_detaModel.photoImgs componentsSeparatedByString:@","];
    [_bannerAry addObjectsFromArray:banAry];
    NSArray *comAry = [self stringToJSON:_detaModel.companyImgs];
    if (comAry.count > 0)
    {
        for (NSDictionary *dic  in comAry)
        {
            NSString *img = dic[@"img"];
            NSString *tit = dic[@"title"];
            [_companyAry addObject:img];
            [_contentAry addObject:tit];
        }
    }
    _streeID = s_str(_detaModel.street.ID);
    _latitude = _detaModel.lat;
    _lontude = _detaModel.lng;
   

    
}

- (NSArray *)stringToJSON:(NSString *)jsonStr
{
    if (jsonStr)
    {
        id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        if (tmp) {
            if ([tmp isKindOfClass:[NSArray class]]) {
                
                return tmp;
                
            } else if([tmp isKindOfClass:[NSString class]]
                      || [tmp isKindOfClass:[NSDictionary class]]) {
                
                return [NSArray arrayWithObject:tmp];
                
            } else {
                return nil;
            }
        } else {
            return nil;
        }
        
    } else {
        return nil;
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag >= 40000)
    {
        NSInteger index = textField.tag%40000;
        if (_contentAry.count > index)
        {
            [_contentAry insertObject:textField.text atIndex:index];
        }
    }
    if (textField.tag == 101)
    {
        _fullName = textField.text;
    }
    else if (textField.tag == 102)
    {
        _ReferredName = textField.text;
    }
    else if (textField.tag == 103)
    {
        _contact = textField.text;
    }
    else if (textField.tag == 104)
    {
        _phoneStr = textField.text;
    }
    else if (textField.tag == 105)
    {
        _MachineStr = textField.text;
    }
    else if (textField.tag == 106)
    {
        _addressStr = textField.text;
    }
    else if (textField.tag == 107)
    {
        _websiteStr = textField.text;
    }
    else if (textField.tag == 108)
    {
        _businStr = textField.text;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    return indexPath;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
}

//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.textColor = BlaTitColor;
    if ([textView.text isEqualToString:@"请用尽量多的文字，详细的描述您公司的企业文化..."])
    {
        textView.text= @"";
    }
}



//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""])
    {
        textView.text= @"请用尽量多的文字，详细的描述您公司的企业文化...";
        textView.textColor = RGB(179, 179, 179);
    }
    _pladerStr = textView.text;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        //在这里做你响应return键的代码
        [self.view endEditing:YES];
        return NO; //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
